package com.cmnit.utils;

import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MySqlUtils {
    private static Logger logger = Logger.getLogger(MySqlUtils.class);

    /**
     * 查询数据
     *
     * @param sql         SQL语句
     * @param resultField 结果字段
     * @return 结果集
     */
    public static List<String> query(String sql, String resultField) {
        String[] resultFields = resultField.split(",");
        List<String> result = new ArrayList<>();
        Connection connect = null;
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        try {
            connect = DruidUtils.getConnection();
            pstmt = connect.prepareStatement(sql);
            resultSet = pstmt.executeQuery();
            while (resultSet.next()) {
                StringBuilder sb = new StringBuilder();
                for (String field : resultFields) {
                    sb.append(resultSet.getString(field)).append(",");
                }
                result.add(sb.deleteCharAt(sb.length() - 1).toString());
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return result;
        } finally {
            close(connect, pstmt, resultSet);
        }
    }

    /**
     * 查询数据
     *
     * @param sql         SQL语句
     * @param valuesField 注入字段数据
     * @param resultField 结果字段
     * @return 结果集
     */
    public static List<String> query(String sql, String valuesField, String resultField) {
        String[] valuesFields = valuesField.split(",");
        String[] resultFields = resultField.split(",");
        List<String> result = new ArrayList<>();
        Connection connect = null;
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        String resultData;
        try {
            connect = DruidUtils.getConnection();
            pstmt = connect.prepareStatement(sql);
            for (int i = 0; i < valuesFields.length; i++) {
                resultData = valuesFields[i];
                pstmt.setString(i + 1, resultData);
            }
            resultSet = pstmt.executeQuery();
            while (resultSet.next()) {
                StringBuilder sb = new StringBuilder();
                for (String field : resultFields) {
                    sb.append(resultSet.getString(field)).append(",");
                }
                result.add(sb.deleteCharAt(sb.length() - 1).toString());
            }
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            return result;
        } finally {
            close(connect, pstmt, resultSet);
        }
    }

    /**
     * 插入数据
     *
     * @param tableName   表名
     * @param updateField 字段名
     * @param valuesField 数据
     */
    public static void insert(String tableName, String updateField, String valuesField) {
        Connection connect = null;
        PreparedStatement pstmt = null;
        String[] updateFields = updateField.split(",");
        String[] valuesFields = valuesField.split(",");
        String resultData;
        try {
            // 获取连接对象
            connect = DruidUtils.getConnection();
            connect.setAutoCommit(false);
            String sql = "insert into " + tableName + "(" + updateField + ") values(";
            for (int i = 1; i < updateFields.length; i++) {
                sql = sql + "?,";
            }
            sql = sql + "?)";
            // System.out.println(sql);
            pstmt = connect.prepareStatement(sql);
            for (int i = 0; i < updateFields.length; i++) {
                resultData = valuesFields[i];
                pstmt.setString(i + 1, resultData);
                logger.info("值：" + resultData);
            }
            pstmt.executeUpdate();
            connect.commit();
            System.out.println("写入数据：" + valuesField);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(connect, pstmt, null);
        }
    }

    /**
     * 关闭连接
     *
     * @param conn 连接池连接
     * @param ps   预处理连接
     * @param rs   结果连接
     */
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
